<template>
  <div style="display: flex;height: 50px;
  line-height: 50px;background-color: #409EFF">
    <div style="width: 300px; padding-left: 20px;
    font-weight: bold; color:white;font-size: 30px">
      <i class="el-icon-cloudy"></i>
      后台管理
    </div>
    <div style="flex: 1;"></div>
    <div style="width: 100px;">
      <el-dropdown style="color: white;font-size: 20px">
                <span class="el-dropdown-link">
                  <i class="el-icon-user"></i> {{ user.username }}
                </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="open">个人信息</el-dropdown-item>
            <el-dropdown-item divided @click="exit">退出系统</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
  <!-- 新增弹窗 -->
  <el-dialog title="个人信息" v-model="dialogVisible" width="30%">
    <el-form :model="form" label-width="120px">
      <el-form-item label="用户姓名">
        <el-input v-model="form.username" style="width: 80%;"></el-input>
      </el-form-item>
      <el-form-item label="用户密码">
        <el-input v-model="form.password" style="width: 80%;"></el-input>
      </el-form-item>
      <el-form-item label="用户昵称">
        <el-input v-model="form.nickName" style="width: 80%;"></el-input>
      </el-form-item>
      <el-form-item label="用户年龄">
        <el-input v-model="form.age" style="width: 80%;"></el-input>
      </el-form-item>
      <el-form-item label="用户性别">
        <el-radio v-model="form.sex" label="男">男</el-radio>
        <el-radio v-model="form.sex" label="女">女</el-radio>
        <el-radio v-model="form.sex" label="保密">保密</el-radio>
      </el-form-item>
      <el-form-item label="用户地址">
        <el-input type="textarea" v-model="form.address" style="width: 80%;"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
                    <span class="dialog-footer">
                        <el-button @click="dialogVisible = false">取 消</el-button>
                        <el-button type="primary" @click="save">确 定</el-button>
                    </span>
    </template>
  </el-dialog>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Header",
  data() {
    return {
      form: {},
      dialogVisible: false,
      user: {
        username: "未登录",
      },
    }
  },
  created() {
    // 读取sessionStorage
    let userJson = sessionStorage.getItem("user")
    if (userJson) {
      this.user = JSON.parse(sessionStorage.getItem("user"))
      // console.log(this.user)
    }
  },
  methods: {
    exit() {
      sessionStorage.removeItem("user")
      this.$router.push('/login')
    },
    open() {
      this.form = this.user
      this.dialogVisible = true
    },
    // 点击保存触发
    save() {
      request.put("/users", this.form).then(res => {
        console.log(res)
        if (res.codes === '200') {
          this.$message({
            type: "success",
            message: "更新成功"
          })
        } else {
          this.$message({
            type: "error",
            message: "更新失败"
          })
        }
        //  更新sessionStorage
        sessionStorage.setItem("user", JSON.stringify(this.form))
        //关闭弹窗
        this.dialogVisible = false
      })
    },
  }
}
</script>

<style>
</style>
